package com.QK.cnstudy.entity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Author {
    private int id;
    private String name;
    private String info;
    private String work;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }

    public Author() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public boolean isExist(SQLiteDatabase sqLiteDatabase) {
        Cursor cursor = sqLiteDatabase.rawQuery("select * from author  where id = ?",
                        new String[] { Integer.toString(getId()) });
        if (cursor.moveToNext()) {
            return true;
        }
        return false;
    }

    public void saveToDB(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("insert into author values(?,?,?)", new Object[] { getId(), getName(), getInfo() });
    }

    public Author(String[] strings) {

        int len = strings.length;
        name = (strings[0]);
        info = len > 1 ? strings[1] : "";
        work = len > 2 ? strings[2] : "";
    }

    public void setInfoFromCursor(Cursor cursor) {
        int tmp;
        if ((tmp = cursor.getColumnIndex("name")) != -1) {
            setName(cursor.getString(tmp));
        }
        if ((tmp = cursor.getColumnIndex("info")) != -1) {
            setInfo(cursor.getString(tmp));
        }
        if ((tmp = cursor.getColumnIndex("work")) != -1) {
            setWork(cursor.getString(tmp));
        }

    }
}
